package com.yuxin.tongyi.lsll.util;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;

import com.yuxin.tongyi.lsll.main.LoginActivity;
import com.yuxin.tongyi.lsll.pojo.ClassInfo;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.widget.Toast;

/**
 * @author ww
 * @date:2011-12-16 下午04:59:29
 * @Desc:
 * @version :
 * 
 */
public class PubUtil {

	static String filename = "config";
	static SharedPreferences preferences;
	static SharedPreferences.Editor editor;
	
	
	public static SharedPreferences getPreferencesInstance(Context context){
		if(preferences == null){
			preferences = context.getSharedPreferences(filename, Context.MODE_PRIVATE);
		}
		return preferences;
	}
	/**
	 * 获得参数
	 * @param context
	 * @param fileName
	 * @param key
	 * @param value
	 * @return
	 */
	public static String getConfigValue(Context context, String key, String defvalue){
		return getPreferencesInstance(context).getString(key, defvalue);
	}
	
	public static int getConfigValue(Context context, String key, int defvalue){
		return Integer.parseInt(getConfigValue(context, key, Integer.toString(defvalue)));
	}
	
	public static boolean getConfigValue(Context context, String key, boolean defvalue){
		return new Boolean(getConfigValue(context, key, Boolean.toString(defvalue)));
	}
	
	/**
	 * 保存参数
	 * @param context
	 * @param fileName
	 * @param key
	 * @param value
	 */
	public static void setConfigValue(Context context, String key, String value){
		editor = getPreferencesInstance(context).edit();
		editor.putString(key, value);
		editor.commit();
	}
	
	public static void setConfigValue(Context context, String key, int value){
		setConfigValue(context, key, Integer.toString(value));
	}
	
	public static void setConfigValue(Context context, String key, boolean value){
		setConfigValue(context, key, Boolean.toString(value));
	}
	
	/**
	 * 删除参数值
	 * @param context
	 * @param key
	 */
	public static void removeConfigValue(Context context, String key){
		editor = getPreferencesInstance(context).edit();
		editor.remove(key);
		editor.commit();
	}
	
	/**
	 * ProgressDialog
	 */
	private static ProgressDialog progressDialog;
	
	public static void showProgressDialog(Context context, CharSequence title, CharSequence content){
		progressDialog = new ProgressDialog(context);
		progressDialog.setTitle(title);
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setMessage(content);
		progressDialog.setIndeterminate(true);
		progressDialog.setCancelable(true);
		progressDialog.show();
	}
	
	public static void closeProgressDialog(){
		if(progressDialog.isShowing()){
			progressDialog.dismiss();
		}
	}
	
	/**
	 * Toast
	 */
	public static void showToast(Context context, CharSequence text){
		Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
	}
	
	public static void showToastLong(Context context, CharSequence text){
		Toast.makeText(context, text,Toast.LENGTH_LONG).show();
	}
	
	/**
	 * Dialog
	 */
	public static void showDialog(Context context, String title, String hint, DialogInterface.OnClickListener Poslistener, boolean isNegBtn){
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
		alertDialog.setTitle(title);
		alertDialog.setMessage(hint);
		alertDialog.setPositiveButton("确定", Poslistener);
		if(isNegBtn){
			alertDialog.setNegativeButton("取消", null);
		}
		alertDialog.show();
	}
	
}
